package com.dapeis.web.wx.wx.handler;

import java.util.Map;

import javax.annotation.Resource;

import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.bean.outxmlbuilder.NewsBuilder;

import org.springframework.stereotype.Component;

import com.ag.core.base.Pager;
import com.ag.utils.CollectionUtils;
import com.dapeis.domain.User;
import com.dapeis.service.UserService;
import com.dapeis.service.UserThirdPartyLoginService;


/**
 * 微信取消关注事件处理
 * Title:WxEventMyOrderHandler.java WxEventMyOrderHandler
 * Description:
 * @author yuanyong
 * @date 2017年4月18日 上午9:20:27 
 * Email: hzyuanyong@126.com
 *
 */
@Component
public class WxEventMyOrderHandler implements WxMpMessageHandler {
	@Resource UserService userService;
	@Resource UserThirdPartyLoginService userThirdPartyLoginService;
	
	private WxMpXmlOutMessage setInfo(Pager pager, WxMpXmlMessage wxMessage) {
		if(CollectionUtils.isNotEmpty(pager.getItemList())) {
			WxMpXmlOutNewsMessage.Item itemTitle = new WxMpXmlOutNewsMessage.Item();
			itemTitle.setPicUrl("url/guanzhu2.jpg");
			itemTitle.setTitle("点击查看全部订单 >");
			//itemTitle.setUrl(FileUtils.splicePaths(XDMCfgUtils.getWxDomain(), "v1/tooth_beauty/my_order?t=" + System.currentTimeMillis()));
			
			WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
			
			NewsBuilder newsBuilder = WxMpXmlOutMessage.NEWS().addArticle(itemTitle).addArticle(item);
			WxMpXmlOutNewsMessage m = newsBuilder.fromUser(wxMessage.getToUserName()).toUser(wxMessage.getFromUserName()).build();
			return m;
		} else {
			String emptyText = "订单信息为空.";
			WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT()
					.content(emptyText)
					.fromUser(wxMessage.getToUserName())
					.toUser(wxMessage.getFromUserName()).build();
			return m;
		}
	}

	@Override
	public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
			Map<String, Object> context, WxMpService wxMpService,
			WxSessionManager sessionManager) {

		String openId = wxMessage.getFromUserName();
		//User user = userService.getUserByWeixinOpenId(openId);
		User user = null;
		if (user == null) {
			WxMpXmlOutTextMessage m = WxMpXmlOutMessage.TEXT()
					.content("系统错误，找不到该用户的信息!")
					.fromUser(wxMessage.getToUserName())
					.toUser(wxMessage.getFromUserName()).build();
			return m;
		}

		//Pager pager = new Pager(Order.class, new OrderPagerSearchConvertCriteria());
		Pager pager = null;
		pager.setPageSize(1);
		//pager.getSearchMap().put("source", Order.SOURCE_WX);

		pager.getSearchMap().put("userId", String.valueOf(user.getId()));
		//orderService.pager(pager);
		return setInfo(pager, wxMessage);
	}

}
